// 角色设置
function startRoleSetting() {
    var arr = getCheckedIdArr();
    if (!arr.length > 0) {
        layer.alert('请选择用户！',{icon: 5});
        return;
    }
    openPage('职位分配', roleSettingUrl, function () {
        // 显示复选框样式
        showIcheckCss();
        $("#mainRole").on('ifChecked', function () {
            if ($("#mainRole").val() == 0) {
                layer.alert('请先选择职位，再勾选！', {icon: 5}, function (index) {
                    $("#mainRole").iCheck('uncheck');
                    layer.close(index);
                });
            }
        });
        ajaxPost(ajaxRoleListUrl, {}, function (result) {
            var setting = {
                check: {
                    enable: true
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                view: {
                    showIcon: false,
                    showLine: false,
                    showTitle: false
                },
                callback: {
                    onClick: function (event, treeId, treeNode) {
                        treeObj.checkNode(treeNode, true, true);
                        $("#mainRole").val(treeNode.id);
                        if (treeNode.mainRole == "true") {
                            $('#mainRole').iCheck('check');
                        } else {
                            $('#mainRole').iCheck('uncheck');
                        }
                    }
                }
            };

            var zNodes = eval(result.data);

            var treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            // 显示角色已拥有的菜单
            ajaxPost(ajaxUserRole, {'userId': arr[0]}, function (result) {
                for (var i = 0; i < result.data.length; i++) {
                    var node = treeObj.getNodeByParam("id", result.data[i].roleId, null);
                    if (node != null) {
                        // 角色显示
                        treeObj.checkNode(node, true, true);
                        // 主角色显示
                        if (result.data[i].mainRole == 'true') {
                            $('#mainRole').val(result.data[i].roleId);
                            $('#mainRole').iCheck('check');
                            node.mainRole = "true";
                            // ztree节点增加选中状态
                            treeObj.selectNode(node);
                        }
                    }
                }
            })
        })
    }, function () {
        startSaveSetting(arr[0]);
    });
}

function startSaveSetting(userIds) {
    var roleId = 0;
    var checked = $("#mainRole").prop('checked');
    if (checked) {
        roleId = $("#mainRole").val();
    }
    var arr = getZtreeCheckedIdArr();
    if (!arr.length > 0) {
        layer.alert('请选择职位！',{icon: 5});
        return;
    }
    var roleIds = arr.join(",");
    ajaxPost(ajaxSaveSettingUrl, {"userIds": userIds, "roleIds": roleIds, "roleId": roleId}, function (result) {
        layer.close(layer.index);
        layer.msg(result.msg, {time: 1000}, function () {
            startRefresh();
        });
    });
}